home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr43 / ppl4p10.zip / FILE_IO.PAS < prev    next >
Pascal/Delphi Source File  |  1995-02-20  |  5KB  |  203 lines

  1. UNIT File_IO;
  2.  
  3. INTERFACE
  4. USES Dos;
  5.  
  6. (*  Only ONE file may be open at any one time. This is all that is
  7. **  necessary for protocol support. *)
  8.  
  9. Const BufferSize = 1024;
  10. Type  BufferType = array[0..BufferSize-1] of Byte;
  11.  
  12. Procedure fioInit;
  13. Function  fioFind(Pathname:String; Var Name:String; Var Size,Time:LongInt): Boolean;
  14. Function  fioCreate(Pathname:String): Boolean;
  15. Function  fioOpen(Pathname:String): Boolean;
  16. Procedure fioClose;
  17. Function  fioEOF:Boolean;
  18. Function  fioSize : LongInt;
  19. Function  fioSeek(Position:LongInt): Boolean;
  20. Procedure fioPreRead;
  21. Function  fioRead(Var Buffer:BufferType; BytesWanted:Word; Var BytesRead:Word): Boolean;
  22. Function  fioWrite(Var Buffer:BufferType; Bytes2Write:Word): Boolean;
  23. Procedure fioSetFTime(Time:LongInt);
  24.  
  25. IMPLEMENTATION
  26.  
  27. Const
  28.   PreBuffSize = BufferSize;
  29. Var
  30.   IsOpen   : Boolean;
  31.   TheFile  : File;
  32.   PreBuff  : array[0..PreBuffSize-1] of Byte;
  33.   PreLeft  : Integer;  (* leftmost byte index in PreBuff[] *)
  34.   PreRight : Integer;  (* 1 + rightmost byte index in PreBuff[] *)
  35.   PreIO    : Boolean;
  36.   PreEOF   : Boolean;
  37.  
  38. (* reset file_io sub-system *)
  39.  
  40. Procedure fioInit;
  41. Begin
  42.   IsOpen  := False;
  43.   PreLeft := 0;
  44.   PreRight:= 0;
  45.   PreIO   := True;
  46.   PreEOF  := False
  47. End;
  48.  
  49. (* open an existing file for read *)
  50.  
  51. Function fioOpen(Pathname:String): Boolean;
  52. Begin {$I-}
  53.    if IsOpen then
  54.      begin
  55.        fioOpen := False;
  56.        exit
  57.      end;
  58.    PreEOF := False;
  59.    Assign(TheFile,Pathname);
  60.    Reset(TheFile,1);
  61.    if IOresult = 0 then IsOpen := True;
  62.    fioOpen := (IOresult = 0)
  63. End; {$I+}
  64.  
  65. (* open a file for write *)
  66.  
  67. Function fioCreate(Pathname:String): Boolean;
  68. Begin {$I-}
  69.    if IsOpen then
  70.      begin
  71.        fioCreate := False;
  72.        exit
  73.      end;
  74.    PreEOF := False;
  75.    Assign(TheFile,Pathname);
  76.    ReWrite(TheFile,1);
  77.    if IOresult = 0 then IsOpen := True;
  78.    fioCreate := (IOresult = 0)
  79. End; {$I+}
  80.  
  81. (* close opened file *)
  82.  
  83. Procedure fioClose;
  84. Begin {$I-}
  85.    IsOpen := False;
  86.    Close(TheFile);
  87.    IF (IOresult <> 0) Then
  88.       { ignore result }
  89. End; {$I+}
  90.  
  91. (* position file pointer *)
  92.  
  93. Function fioSeek(Position:LongInt): Boolean;
  94. Begin {$I-}
  95.    if not IsOpen then
  96.      begin
  97.        fioSeek := False;
  98.        exit
  99.      end;
  100.    PreLeft := 0;
  101.    PreRight := 0;
  102.    Seek(TheFile,Position);
  103.    fioSeek := (IOresult = 0)
  104. End; {$I+}
  105.  
  106. (* write to opened file *)
  107.  
  108. Function fioWrite(Var Buffer:BufferType; Bytes2Write:Word):Boolean;
  109. Begin {$I-}
  110.    if not IsOpen then
  111.      begin
  112.        fioWrite := False;
  113.        exit
  114.      end;
  115.    BlockWrite(TheFile,Buffer,Bytes2Write);
  116.    fioWrite := (IOresult = 0)
  117. End; {$I+}
  118.  
  119. (* read into 'read ahead' buffer. Call before having to wait for ACK, etc *)
  120.  
  121. Procedure fioPreRead;
  122. Begin {$I-}
  123.   if IsOpen and (PreLeft = PreRight) then
  124.     begin
  125.       (* fill PreBuff[] *)
  126.       PreLeft := 0;
  127.       BlockRead(TheFile,PreBuff,PreBuffSize,PreRight);
  128.       if PreRight < PreBuffSize then PreEOF := True;
  129.       PreIO := (IOresult = 0)
  130.     end
  131. End; {$I+}
  132.  
  133. (* return EOF flag *)
  134.  
  135. Function fioEOF:Boolean;
  136. Begin
  137.   if (PreLeft = PreRight) and PreEOF then fioEOF := True
  138.   else fioEOF := False;
  139. End;
  140.  
  141. (* read from opened file *)
  142.  
  143. Function fioRead(Var Buffer:BufferType; BytesWanted:Word; Var BytesRead:Word):Boolean;
  144. Var
  145.    i  : Integer;
  146. Begin
  147.    if not IsOpen then
  148.      begin
  149.        fioRead := False;
  150.        exit
  151.      end;
  152.    fioPreRead;
  153.    (* compute # bytes we will return *)
  154.    if BytesWanted > BufferSize then BytesWanted := BufferSize;
  155.    if BytesWanted <= (PreRight-PreLeft) then BytesRead := BytesWanted
  156.    else BytesRead := PreRight - PreLeft;
  157.    (* get the bytes *)
  158.    for i := 0 to BytesRead-1 do Buffer[i] := PreBuff[PreLeft+i];
  159.    PreLeft := PreLeft + BytesRead;
  160.    (* return correct I/O result *)
  161.    if PreLeft = PreRight then fioRead := PreIO
  162.    else fioRead := True
  163. End;
  164.  
  165. (* find file *)
  166.  
  167. Function fioFind(Pathname: String; Var Name: String; Var Size, Time: LongInt): Boolean;
  168. Var
  169.    Info: SearchRec;
  170. Begin {$I-}
  171.    FindFirst(Pathname,Archive,Info);
  172.    IF (DosError <> 0) OR (IOresult <> 0) Then
  173.    Begin
  174.       fioFind := False;
  175.       Exit
  176.    End;
  177.    Name := Info.Name;
  178.    Size := Info.Size;
  179.    Time := Info.Time;
  180.    fioFind := True
  181. End; {$I+}
  182.  
  183. (* get file size *)
  184.  
  185. Function fioSize : LongInt;
  186. Begin
  187.   if IsOpen then fioSize := FileSize(TheFile)
  188.   else fioSize := 0;
  189. End;
  190.  
  191. (* set file time *)
  192.  
  193. Procedure fioSetFTime(Time: LongInt);
  194. Begin {$I-}
  195.    SetFTime(TheFile,Time);
  196.    IF (IOresult <> 0) Then
  197.       {null}
  198. End; {$I+}
  199. Begin
  200.   (* initialize *)
  201.   fioInit
  202. End.
  203.